"auto"

//缩小地图 归位
function resetMap(place){
    setScreenMetrics(1920,1080)
    //扩大 test
    for(let i=0;i<3;i++){
    gestures([500,[705+rr,545+rr],[250+rr,550+rr]],
        [500,[1250+rr,555+rr],[1720+rr,540+rr]])
    sleep(1000)
    }

    log("缩小地图 归位")
    for(let i=0;i<4;i++){
        gestures([500,[250+rr,550+rr],[705+rr,545+rr]],
                [500,[1720+rr,540+rr],[1250+rr,555+rr]])
        sleep(1000+t)
    }
    
    if(place==0)      gestures([500,[666+rr,302+rr],[926+rr,486+rr]])   //拉左上角
    else if(place==1) gestures([500,[1358+rr,240+rr],[1080+rr,473+rr]]) //拉右上角
    else if(place==2) gestures([500,[453+rr,907+rr],[763+rr,700+rr]])   //拉左下角
    else if(place==3) gestures([500,[1480+rr,860+rr],[1124+rr,421+rr]]) //拉右下角
    
}

//随机滑动 点击
function randomClick(){
    log("随机滑动 点击")
    let x1 = 248
    let x2 = 320
    let y1 = 1761
    let y2 = 825
    click(random(x1,y1),random(x2,y2))
    let n =1
    for(let i=0;i<random(8,16);i++){
        if(random(1,2)==1){
            click(random(x1,y1),random(x2,y2))
        }else{
            gestures([350,[random(x1,y1),random(x2,y2)],
                          [random(x1,y1),random(x2,y2)]])
        }
        sleep(random(1321,1421))
        log("n="+n++)
    }
}


//识别敌方金币圣水ocr
function enemyMoneyOCR(){
    var imgPath = "/sdcard/Pictures/money.png"
    log("by:"+1)
    var img = captureScreen()
    log("by:"+2)
    var imgClip = images.clip(img,95,136,189,129)//284 279
    images.save(imgClip,imgPath)
    img.recycle()
    
    var url = "http://192.168.16.172:8080/aaa"
    var result = http.postMultipart(url,{
        file:open(imgPath)
    })
    json = result.body.json()
    log(json.core)
    log(json.mess)
    var m1 = json.mess.split(";")[0]
    var m2 = json.mess.split(";")[1]
    var m3 = json.mess.split(";")[2]
    log("out1 = "+m1)
    log("out2 = "+m2)
    log("out3 = "+m3)
    var res = "no"
    if(m1>2000 ||m2>2000||m3>2000)
        res = "进攻"
    return res;
}

//打鱼
function figthing(){
    sleep(500+t)
    click(70+rrr,908+rrr)//进攻按钮
    sleep(500+t)
    click(1400+rrr,600+rrr)//搜索对手按钮
    while(true){
        sleep(10000+t)
        var result = enemyMoneyOCR()
        if(result=="进攻"){
            log("打鱗了")
            resetMap(1)
            sleep(500+t)
            click(184+rrr,929+rrr)//选择第一种兵
            sleep(500+t)
            for(let i=0;i<bingli;i++){
                var x = parseInt(986+(1553-986)*i/65)
                var y = parseInt(x*0.71-646)
                log("x="+x)
                log("y="+y)
                click(x,y)
                sleep(20)
            }
            sleep(180000+t)
            click(896+rrr,937+rrr)//打完 回营 按钮

            break;
        }else{
            log("少了，下一个")
            click(1694+rrr,743+rrr)//下一个 按钮
        }
    }
}
//造兵
function buildArm(){
    sleep(2000+t)
    click(55+rr,767+rr)//左下角兵种按钮
    sleep(500+t)
    click(1186+rrr,42+rr)//一键训练 按钮
    sleep(500+t)
    click(1683+rr,488+rr)//第一套兵种x2
    sleep(500+t)
    click(1683+rr,488+rr)//第一套兵种x2
    sleep(500+t)
    click(1790+rr,48+rr)//退出
    sleep(2000+t)
} 

//检测金币圣水是否已满
function checkMoneyFull(){
    // log("步骤："+i++)
    var img = captureScreen();
    images.save(img,"/sdcard/Pictures/moneyCapture.png")
    // log("步骤："+i++)
    //金币
    sleep(200+t)
    // 在指定的区域找到指定颜色，x坐标与y坐标必须指定，长宽尺寸和临界值可不指定
    var money = findColorInRegion(img, "#f4dc72",1526,54,2,2,4);
    if(money){
        log("金币满了--找到了,坐标为(" + money.x + ", " + money.y + ")");
        click(money.x,money.y);
    }else{
        log("金币没满--没找到");
    };
    // log("步骤："+i++)
    //圣水
    sleep(200+t)
    // 在指定的区域找到指定颜色，x坐标与y坐标必须指定，长宽尺寸和临界值可不指定
    var water = findColorInRegion(img, "#e18de1",1526,150,2,2,4);
    if(water){
        log("圣水满了--找到了,坐标为(" + water.x + ", " + water.y + ")");
        click(water.x,water.y);
    }else{
        log("圣水没满--没找到");
    };
    // log("步骤："+i++)
    return money&&water;
}

//识别建筑工人
function working(){
    let res = 0;
    var workerPath = "/sdcard/Pictures/worker"
    var img = captureScreen();

    for(let i=1;i<3;i++){
        let wp = workerPath+""+i+".png"
        log("wp==="+wp)
        
        let workerImg = images.read(wp);
        sleep(200+t)

        var result = findImage(img,workerImg,{
            region:[730,25,101,56],//831,81
            threshold:0.8
        });
        
        if(result){
            res = i;
            log("找到啦")
            log(result.x)
            log(result.y)
            click(result.x,result.y)
            sleep(500+t)
            // var imgClip = images.clip(img,result.x+28,result.y+133,59,19)
            // images.save(imgClip,"/sdcard/Pictures/wu.png")
            // for(let j=0;j<i;j++){
                //满了就升一个，升多个可能资源不够
                upgradeTheBuilding();
            // }
            break;
        }else{
            log("没找到 " +i+" 个工人")
        };
        
        workerImg.recycle();
    }
    img.recycle();
    return res;
}
//升级建筑
function upgradeTheBuilding(){
    click(641+rrr,29+rrr)//点击建筑工人头像
    sleep(500+t)
    click(755+r,413+r)//点击其他升级第一个
    sleep(500+t)
    click(1026+rrr,841+rrr)//升级按钮
    sleep(500+t)
    click(919+rrr,919+rrr)//使用资源按钮
    sleep(500+t)
}

//实验室 TODO
function laboratory(){
    resetMap(1)
    click(1844+rr,438+rr)
    sleep(500+t)

    var imgPath = "/sdcard/Pictures/study.png"
    var img = captureScreen();
    var template= images.read(imgPath);
    // var result = findImage(img,template);
    var result = findImage(img,template,{
        region:[780,88,150,80],//831,81
        threshold:0.8
    });//902 144
    if(result){
        log("实验 找到啦")
        log(result.x)
        log(result.y)

        sleep(500+t)
        //打开实验室
        click(852+r,155+r)//实验室
        sleep(500+t)
        click(1135+rrr,846+rrr)//研究 按钮
        sleep(500+t)

        //检测可以升级的兵种
        //435 940
        var img = captureScreen()
        //230 -30593
        for(let i=0;i<5;i++){
            //第一行
            var dot = img.pixel(435+(230*i),713)
            log("dot:"+dot)
            if(dot == -1){
                click(435+(230*i)-rrr,713-rrr)//点击兵种
                sleep(500+t)
                // click(1438+rrr,930+rrr)//升级
                break
            }

            //第二行
            var dot = img.pixel(435+(230*i),940)
            log("dot:"+dot)
            if(dot == -1){
                click(435+(230*i)-rrr,940-rrr)//点击兵种
                sleep(500+t)
                // click(1438+rrr,930+rrr)//升级
                break
            }
            // var color = colors.toString(dot)
            // log("color:"+color)
            // click(435+(230*i),940)
        }
        
        img.recycle()
        
    }else{
        log("实验 没找到")
    };

    
}





function redTag(){
    click(783+r,417+r)//关闭聊天窗口
    var img = captureScreen()
    //
    var dot = img.pixel(45,410)
    log("dot:"+dot)
    // if(dot==-3143656){//有新消息
    if(true){//有新消息
        sleep(500+t)
        click(15+r,442+rr)//打开 消息 界面
        sleep(500+t)
        let hua = 0;
        
        //请求模块
        qingqiu2()
         

        sleep(2000+t)
        click(781+r,421+r)//关闭聊天界面
        sleep(2000+t)
        click(48+rr,755+rr)//左下角 兵营 按钮
        sleep(2000+t)

        //造兵s
        log("lennnn=="+createArms.length)
        let createArmsLen = createArms.length
        if(createArmsLen!=0){
            sleep(2000+t)
            click(493+rr,41+rr)//训练部队 按钮
            sleep(2000+t)
        }
        for(let i=0;i<createArmsLen;i++){
            createArm(createArms.pop())
        }

        //造法术s
        let createMagicsLen = createMagics.length
        if(createMagicsLen!=0){
            sleep(2000+t)
            click(870+rr,54+rr)//配置法术 按钮
            sleep(2000+t)
        }
        for(let i=0;i<createMagicsLen;i++){
            createMagic(createMagics.pop())
        }
    }
}

var path = "/sdcard/Pictures/"
var arms = ["qiqiu","xueguai","gebulin"]
var magics = ["crazy","ice"]
var cars = []
var createArms = []
var createMagics = []
var createCars = []

// 查找 请求字样
function qingqiu(){
    let res = 0
    // click(140,477);
    var imgPath = "/sdcard/Pictures/qingqiu.png"//救命呀！我需要增援！
    var img = captureScreen();
    var template= images.read(imgPath);
    // var result = findImage(img,template);
    var result = findImage(img,template,{
        region:[89,548,340,389],//429 937
        threshold:0.8
    });//739,668
    
    if(result){
        log("请求 找到啦")
        log(result.x)
        log(result.y)
        res = 1

        let hight = 310;
        log(result.y+hight>device.height)
        log(device.height)
        if(result.y+hight>device.height)
            hight = device.height - result.y;
        log("hight="+hight)
        let zimga = captureScreen();
        var zimg= images.read("/sdcard/Pictures/zengyuan.png");//增援 按钮
        var zres = findImage(zimga,zimg,{
            region:[result.x,result.y,620,hight],//720-100 925-615
            threshold:0.8
        });//739,668

        if(zres){
            log("增援 按钮 找到了")
            let zlenx = zres.x - result.x;
            let zleny = zres.y - result.y;
            // log(zlenx)
            // log(zleny)
            //click(zres.x,zres.y)//点击 增援 按钮
            if(zleny<100){
                log("无要求增援")
                //无要求的增援
                zengyuanHas(result,0)
            }else{
                log("有要求增援")
                //有要求的增援
                zengyuanHas(result,1)
            }

        }else{
            log("增援 按钮 没找到")
        }
        zimga.recycle()
        zimg.recycle()

    }else{
        log("请求 没找到")
    };

    //174 743  233 762
    //+28  +133  59  19
    img.recycle();
    template.recycle();
    return res
}
function qingqiu2(){
    
    let zimg= images.read("/sdcard/Pictures/zengyuan.png");//增援 按钮

    //首页捐兵
    log("首页捐兵")
    let x1 = 496
    let y1 = 715
    let x2 = 731
    let y2 = 971
    let la = 0
    
    while(la<4){
        let zN = 1
        let x0 = x1
        let y0 = y1-la*200
        let w = x2-x0
        let h = y2-y0
        log("la=="+la)
        log(x0,y0,w,h)
        let zimga = captureScreen();
        let zres = findImage(zimga,zimg,{
            region:[x0,y0,w,h],
            threshold:0.8
        });
        zimga.recycle()
        if(zres){
            log("增援按钮 找到了")
            zN = zengyuanNot(zres)
            // zN=0
        }else{
            log("增援按钮 没找到")
            la++
        }
        if(zN==0){
            y2=y0
            la++
        }
    }
    

    //角标 捐兵
    log("角标捐兵")
    let jimg= images.read("/sdcard/Pictures/jiaobiao.png");//角标
    sleep(2000+t)
    do{
        let jimga = captureScreen();
        let jiao = findImage(jimga,jimg,{
            region:[701,107,36,50],//737 157
            threshold:0.8
        });
        jimga.recycle()
        
        if(jiao){
            log("角标 找到了")
            click(jiao.x,jiao.y)//点击 角标
            sleep(2000+t)
            let zimga2 = captureScreen();
            let zres2 = findImage(zimga2,zimg,{
                region:[496,100,238,426],//734 526
                threshold:0.8
            });
            zimga2.recycle()

            if(zres2){
                log("增援按钮 找到了")
                zN = zengyuanNot(zres2)
            }else{
                log("增援按钮 没找到")
            }
        }else{
            log("角标 没找到")
            break
        }
    }while(true)
    jimg.recycle()

    zimg.recycle()
}

function zengyuanHas(result,has){
    
    if(has==0)          click(result.x+474+r,result.y+68+r)//点击 增援 按钮（无需求）//97 798 585 866
    else if(has ==1)    click(result.x+474+r,result.y+263+r)//点击 增援 按钮（有需求）

    //arm
    for(let i=0;i<9;i++){
        if(noArm==arms.length) break;//一轮后都我都没有兵总，跳出循环
        log("开始找 arm:"+i)
        for(let j=0;j<arms.length;j++){
            log("====start======")
            log(path+arms[j]+".png")
            
            sleep(500+t)
                
            //查找 我 有没有 该兵种
            let armMe = images.read(path+arms[j]+"1.png")
            let armMeAll = captureScreen()
            sleep(1000+t)
            let meIs = findImage(armMeAll,armMe,{
                region:[783,20,1660-783,868-20],
                threshold:0.95
            });
            if(meIs){
                log(arms[j]+" 我有 这兵种")
                log(meIs.x)
                log(meIs.y)

                sleep(1000+t)
                //567,873
                click(meIs.x,meIs.y)//增援 该兵种
                createArms.push(arms[j])
                sleep(1000+t)
                
                armMe.recycle()
                armMeAll.recycle()
                noArm = 0;
                break;
            }else{
                log(arms[j]+" 我没 这兵种")
                noArm ++;
            }
            armMe.recycle()
            armMeAll.recycle()            
            log("====end========")
        }
    }
    
    
    //magic
    for(let i=0;i<3;i++){
        if(noMagic==magics.length) break;//一轮后都我都没有兵总，跳出循环
        log("开始找 majic:"+i)
        for(let j=0;j<magics.length;j++){
            log(path+magics[j]+".png")
            sleep(500+t)
                
            //查找 我 有没有 该兵种
            let magicMe = images.read(path+magics[j]+"1.png")
            let magicMeAll = captureScreen()
            sleep(1000+t)
            let meIs = findImage(magicMeAll,magicMe,{
                region:[783,20,1660-783,868-20],
                threshold:0.95
            });
            if(meIs){
                log(magics[j]+" 我有 这法术")
                log(meIs.x)
                log(meIs.y)
                sleep(1000+t)
                //567,873
                click(meIs.x,meIs.y)//增援 该兵种
                createMagics.push(magics[j])
                sleep(1000+t)
                
                magicMe.recycle()
                magicMeAll.recycle()
                noMagic=0
                break;
            }else{
                noMagic++
                log(magics[j]+" 我没 这法术")
            }
            magicMe.recycle()
            magicMeAll.recycle()    
        }
    }
    click(1819+rrr,443+rrr)//关闭我的增援界面
    
}

function zengyuanNot(res){
    let zN = 0
    sleep(2000+t)
    click(res.x,res.y)//点击增援按钮
    sleep(2000+t)

    //arm
    let noArm = 0;
    for(let i=0;i<9;i++){
        if(noArm==arms.length) break;//一轮后都我都没有兵总，跳出循环
        log("开始找 arm:"+i)
        for(let j=0;j<arms.length;j++){
            log("====start======")
            log(path+arms[j]+".png")
            
            sleep(500+t)
                
            //查找 我 有没有 该兵种
            let armMe = images.read(path+arms[j]+"1.png")
            let armMeAll = captureScreen()
            sleep(1000+t)
            let meIs = findImage(armMeAll,armMe,{
                region:[783,20,1660-783,868-20],
                threshold:0.98
            });
            if(meIs){
                log(arms[j]+" 我有 这兵种")
                zN++

                sleep(1000+t)
                //567,873
                click(meIs.x,meIs.y)//增援 该兵种
                createArms.push(arms[j])
                sleep(1000+t)
                
                armMe.recycle()
                armMeAll.recycle()
                noArm = 0;
                break;
            }else{
                log(arms[j]+" 我没 这兵种")
                noArm ++;
            }
            armMe.recycle()
            armMeAll.recycle()            
            log("====end========")
        }
    }
    
    
    //magic
    let noMagic = 0;
    for(let i=0;i<3;i++){
        if(noMagic==magics.length) break;//一轮后都我都没有兵总，跳出循环
        log("开始找 majic:"+i)
        for(let j=0;j<magics.length;j++){
            log(path+magics[j]+".png")
            sleep(500+t)
                
            //查找 我 有没有 该兵种
            let magicMe = images.read(path+magics[j]+"1.png")
            let magicMeAll = captureScreen()
            sleep(1000+t)
            let meIs = findImage(magicMeAll,magicMe,{
                region:[783,20,1660-783,868-20],
                threshold:0.95
            });
            if(meIs){
                log(magics[j]+" 我有 这法术")
                zN++
                sleep(1000+t)
                //567,873
                click(meIs.x,meIs.y)//增援 该兵种
                createMagics.push(magics[j])
                sleep(1000+t)
                
                magicMe.recycle()
                magicMeAll.recycle()
                noMagic=0
                break;
            }else{
                noMagic++
                log(magics[j]+" 我没 这法术")
            }
            magicMe.recycle()
            magicMeAll.recycle()    
        }
    }

    //car
    let noCar = 0;

    click(1819+rrr,443+rrr)//关闭我的增援界面
    return zN
}


function createArm(arm){
    
    for(let i=0;i<arms.length;i++){
        if(arm == arms[i]){
            var armImage = "/sdcard/Pictures/"+arm+"2.png"
            log("armImage:"+armImage)
            var armImg = images.read(armImage)
            sleep(2000+t)
            while(true){
                var img = captureScreen();
                sleep(2000+t)
                
                var res = findImage(img,armImg,{
                    region:[82,549,1756,420],//831,81
                    threshold:0.8
                });
                if(res){
                    log(arm+" 找到啦")
                    log(res.x)
                    log(res.y)
                    click(res.x+r,res.y+r)
                    img.recycle()
                    break
                }else{
                    log(arm+" 没找到")
                    gestures([500,[1362+r,761+r],[550+r,775+r]])//向右滑动
                    img.recycle()
                }
                
            }
            armImg.recycle()
            
        }
    }
}

function createMagic(magic){
    for(let i=0;i<magics.length;i++){
        if(magic == magics[i]){
            var magicImage = "/sdcard/Pictures/"+magic+"2.png"
            log("magicmage:"+magicImage)
            var magicImg = images.read(magicImage)
            sleep(2000+t)
            while(true){
                var img = captureScreen();
                sleep(2000+t)
                
                var res = findImage(img,magicImg,{
                    region:[82,549,1756,420],//831,81
                    threshold:0.8
                });
                if(res){
                    log(magic+" 找到啦")
                    log(res.x)
                    log(res.y)
                    click(res.x+r,res.y+r)
                    img.recycle()
                    break
                }else{
                    log(magic+" 没找到")
                    gestures([500,[1362+r,761+r],[550+r,775+r]])//向右滑动
                    img.recycle()
                }
                
            }
            magicImg.recycle()
            
        }
    }
}


//main
function main(){
    /*
    //1.启动coc
    var coc = getPackageName("部落冲突")
    launch(coc)
    sleep(20000)

    //防止进入后正在被打，加延迟，加被打完后点击
    sleep(180000)
    click(83+rrr,915+rrr)
    sleep(500+t)
    click(1818+rr,69+rr)
    sleep(2000+t)
    
    // click(1844+r,438+r)
    
    // 2.随机点击
    randomClick()
    log("步骤："+i++)
    
    //resetMap(3)
   
    // 4.识别金币圣水
    let full = checkMoneyFull()
    if(full){
        log("full")
        //5.建筑升级
        let workNum = 0
        workNum = working()
        
        if(workNum != 0){//unfull 工人用了
            //6.打鱼
            figthing()
            //7.造兵
            buildArm()
            sleep(2000+t)
            //6.打鱼
            figthing()
            //7.造兵
            buildArm()
        }
    }else{//unfull 
        //6.打鱼
        figthing()
        //7.造兵
        buildArm()
        sleep(2000+t)
        //6.打鱼
        figthing()
        //7.造兵
        buildArm()
    }
    
    home()
    */

    //test
    if(true){
        redTag()
        return 
    }

    let now = new Date()
    let hour = now.getHours()
    let minute = now.getMinutes()
    let second = now.getSeconds()
    log(hour+":"+minute+":"+second)
    

    if((hour>=0&&hour<2)||(hour>=7&&hour<24)){
        while(minute%10!=ranMinute){
            log(minute)
            log(ranMinute)
            now  = new Date()
            minute = now.getMinutes()
        }
        {
            //1.启动coc
            var coc = getPackageName("部落冲突")
            launch(coc)
            sleep(20000)

            sleep(3*60*1000)//3m

             //复位
             click(1840,481)
             click(1840,481)
             click(1840,481)

            //test
            redTag()

            sleep(1*60*1000)//1m
            home()
        }

    }

    
}

//请求截图权限
if(!requestScreenCapture(false)){
    toast("请求截图失败")
    exit()
}
sleep(1000)

var bingli = 70



while(true){
    var i = 0;
    var r = random(0,5)
    var rr = random(0,20)
    var rrr = random(0,50)
    var t = random(200,500)
    var ranMinute = random(1,9)
    

    main();
    sleep(30*60*1000)//30m
}


// openConsole();

